﻿package plugin.roleN.controller
{
    import base.*;
    import cmd.*;
    import com.core.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.component.view.*;
    import plugin.roleN.compoment.*;
    import pluginfw.nio.*;
    import uiresource.roleui.*;
    import view.mornExtends.*;
    import view.uilt.*;

    public class FashionCtrl extends FashionUI
    {
        private var cfgFashion:Array;
        private var cfgItem:Object;
        private var selectObj:Object;
        private var jihuoList:Array;
        private var listArr:Array;
        public var model:RoleShow;
        private var mPowerNumber:NumberComponent;
        private var zuoqiArr:Array;
        private var huanwuArr:Array;
        private var isRide:Boolean = false;
        private var curtime:int;
        private var timer:Timer;
        private var _timeID:uint = 0;
        private var day:int = 0;
        private var oldModel:String = "";

        public function FashionCtrl()
        {
            this.cfgFashion = ConfigDictionary.data.fashion;
            this.cfgItem = ConfigDictionary.data.item;
            this.jihuoList = new Array();
            this.listArr = [];
            this.zuoqiArr = [];
            this.huanwuArr = [];
            return;
        }// end function

        override protected function createChildren() : void
        {
            viewClassMap["uiresource.roleui.FashionItemUI"] = FashionItemCtrl;
            loadUI("Fashion.xml");
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            this.mPowerNumber = new NumberComponent();
            this.mPowerNumber.x = 184;
            this.mPowerNumber.y = 387 + 48;
            this.mPowerNumber.number = 0;
            this.addChild(this.mPowerNumber);
            rideBtn.visible = false;
            chakanLabel.visible = false;
            if (Config.hexie)
            {
                this.txttitle.text = "时装列表";
            }
            this.initEvent();
            return;
        }// end function

        private function initEvent() : void
        {
            tab_shizhuang.selectHandler = new Handler(this.tabHandler);
            rideBtn.addEventListener(MouseEvent.CLICK, this.rideHandler);
            closeBtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            chakanLabel.addEventListener(MouseEvent.CLICK, this.chakanHandler);
            list.renderHandler = new Handler(this.listRender);
            list.selectHandler = new Handler(this.listHandler);
            return;
        }// end function

        public function openRefresh(param1:int) : void
        {
            tab_shizhuang.selectedIndex = 0;
            var _loc_2:* = new Output();
            _loc_2.writeVarInt(6, false);
            PluginManager.instance.sendToBufferServer("fashion", CMDFashion.Init, _loc_2);
            var _loc_3:* = new Output();
            _loc_3.writeVarInt(2, false);
            PluginManager.instance.sendToBufferServer("fashion", CMDFashion.Init, _loc_3);
            return;
        }// end function

        private function tabHandler(param1:int) : void
        {
            this.showListInfo();
            return;
        }// end function

        public function initShowJiHuo(param1:Object) : void
        {
            if (param1.fashionType == 6)
            {
                this.zuoqiArr = param1.fashions;
            }
            if (param1.fashionType == 2)
            {
                this.huanwuArr = param1.fashions;
            }
            this.showListInfo();
            return;
        }// end function

        private function showListInfo() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Object = null;
            if (tab_shizhuang.selectedIndex == 0)
            {
                this.jihuoList = this.zuoqiArr;
            }
            else
            {
                this.jihuoList = this.huanwuArr;
            }
            if (this.listArr != null)
            {
                while (this.listArr.length > 0)
                {
                    
                    this.listArr.pop();
                }
            }
            if (this.zuoqiArr.length > 0 || this.huanwuArr.length > 0)
            {
                rightCon.visible = true;
                timeLabel.visible = true;
                _loc_1 = 0;
                while (_loc_1 < this.jihuoList.length)
                {
                    
                    _loc_2 = new Object();
                    _loc_2 = this.cfgFashion[this.jihuoList[_loc_1].fashionId];
                    _loc_2.display = this.jihuoList[_loc_1].display;
                    _loc_2.fashionId = this.jihuoList[_loc_1].fashionId;
                    _loc_2.fashionType = this.jihuoList[_loc_1].fashionType;
                    _loc_2.outTime = this.jihuoList[_loc_1].outTime;
                    this.listArr[_loc_1] = _loc_2;
                    _loc_1++;
                }
                this.showList(this.listArr);
            }
            else
            {
                timeLabel.visible = false;
                rightCon.visible = false;
                if (tab_shizhuang.selectedIndex == 0)
                {
                    this.listHandler(0);
                }
                else
                {
                    this.listHandler(15);
                }
            }
            this.showTotalShuXing();
            return;
        }// end function

        public function showQiCheng(param1:Object) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:Object = null;
            if (this.selectObj.id == param1.fashionId)
            {
                if (param1.display == 1)
                {
                    rideBtn.label = "取消";
                }
                else
                {
                    rideBtn.label = "使用";
                }
            }
            else
            {
                rideBtn.label = "使用";
            }
            if (param1.fashionType == 6)
            {
                _loc_2 = [];
                _loc_3 = 0;
                while (_loc_3 < this.zuoqiArr.length)
                {
                    
                    if (param1.fashionId == this.zuoqiArr[_loc_3].fashionId)
                    {
                        this.zuoqiArr[_loc_3].display = param1.display;
                    }
                    else
                    {
                        this.zuoqiArr[_loc_3].display = 0;
                    }
                    _loc_4 = new Object();
                    _loc_4 = this.cfgFashion[this.zuoqiArr[_loc_3].fashionId];
                    _loc_4.display = this.zuoqiArr[_loc_3].display;
                    _loc_4.fashionId = this.zuoqiArr[_loc_3].fashionId;
                    _loc_4.fashionType = this.zuoqiArr[_loc_3].fashionType;
                    _loc_4.outTime = this.zuoqiArr[_loc_3].outTime;
                    _loc_2[_loc_3] = _loc_4;
                    _loc_3++;
                }
                this.showList(_loc_2);
            }
            else
            {
                _loc_5 = [];
                _loc_6 = 0;
                while (_loc_6 < this.huanwuArr.length)
                {
                    
                    if (param1.fashionId == this.huanwuArr[_loc_6].fashionId)
                    {
                        this.huanwuArr[_loc_6].display = param1.display;
                    }
                    else
                    {
                        this.huanwuArr[_loc_6].display = 0;
                    }
                    _loc_7 = new Object();
                    _loc_7 = this.cfgFashion[this.huanwuArr[_loc_6].fashionId];
                    _loc_7.display = this.huanwuArr[_loc_6].display;
                    _loc_7.fashionId = this.huanwuArr[_loc_6].fashionId;
                    _loc_7.fashionType = this.huanwuArr[_loc_6].fashionType;
                    _loc_7.outTime = this.huanwuArr[_loc_6].outTime;
                    _loc_5[_loc_6] = _loc_7;
                    _loc_6++;
                }
                this.showList(_loc_5);
            }
            this.showTotalShuXing();
            return;
        }// end function

        private function showList(param1:Array) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_2:Array = [];
            if (list.array != null)
            {
                while (list.array.length > 0)
                {
                    
                    list.array.pop();
                }
            }
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                _loc_2.push(param1[_loc_3]);
                _loc_3++;
            }
            list.array = _loc_2;
            if (this.zuoqiArr.length > 0 || this.huanwuArr.length > 0)
            {
                _loc_4 = ModelProxy.getValue("data", "hdZuoqiId");
                _loc_5 = 0;
                if (_loc_4 != 0 && tab_shizhuang.selectedIndex == 0 && this.zuoqiArr.length > 0)
                {
                    _loc_6 = 0;
                    while (_loc_6 < this.jihuoList.length)
                    {
                        
                        if (int(this.cfgFashion[this.jihuoList[_loc_6].fashionId].item) == _loc_4)
                        {
                            _loc_5 = this.jihuoList[_loc_6].fashionId;
                            break;
                        }
                        _loc_6++;
                    }
                    this.listHandler((_loc_5 - 1));
                }
                else if (tab_shizhuang.selectedIndex == 0)
                {
                    if (this.zuoqiArr.length > 0)
                    {
                        this.listHandler((this.zuoqiArr[0].fashionId - 1));
                    }
                    else
                    {
                        this.listHandler(0);
                    }
                }
                else if (this.huanwuArr.length > 0)
                {
                    this.listHandler((this.huanwuArr[0].fashionId - 1));
                }
                else
                {
                    this.listHandler(15);
                }
            }
            else if (tab_shizhuang.selectedIndex == 0)
            {
                this.listHandler(0);
            }
            else
            {
                this.listHandler(15);
            }
            return;
        }// end function

        private function listRender(param1:Component, param2:int) : void
        {
            if (param2 < list.array.length)
            {
                (param1 as FashionItemCtrl).data = param1.dataSource;
            }
            return;
        }// end function

        private function listHandler(param1:int) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (param1 == -1)
            {
                return;
            }
            clearInterval(this._timeID);
            this.selectObj = null;
            if (list.selectedItem)
            {
                this.selectObj = list.selectedItem;
                rideBtn.visible = true;
                if (list.selectedItem.display == 0)
                {
                    rideBtn.label = "使用";
                }
                else
                {
                    rideBtn.label = "取消";
                }
            }
            else
            {
                this.selectObj = this.cfgFashion[(param1 + 1)];
                if (this.selectObj.display == 0)
                {
                    rideBtn.label = "使用";
                }
                else
                {
                    rideBtn.label = "取消";
                }
                if (this.zuoqiArr.length > 0 && tab_shizhuang.selectedIndex == 0)
                {
                    rideBtn.visible = true;
                }
                else if (this.huanwuArr.length > 0 && tab_shizhuang.selectedIndex == 1)
                {
                    rideBtn.visible = true;
                }
                else
                {
                    rideBtn.visible = false;
                }
            }
            var _loc_2:* = this.selectObj.outTime;
            if (_loc_2 == 0)
            {
                timeLabel.text = "剩余时间：永久";
            }
            else
            {
                _loc_3 = int((ModelProxy.getValue("data", "servertime") + getTimer()) / 1000);
                _loc_4 = _loc_2;
                this.curtime = _loc_4 - _loc_3;
                this.day = this.curtime / 3600 / 24;
                if (this.curtime > 0)
                {
                    this._timeID = setInterval(this.timeHandler, 1000);
                    this.showText(this.curtime);
                }
                else
                {
                    timeLabel.text = "剩余时间：00:00:00";
                }
            }
            this.showCurShuXing();
            this.showCurLimit(param1);
            this.showModel();
            return;
        }// end function

        private function timeHandler() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.curtime - 1;
            _loc_1.curtime = _loc_2;
            this.showText(this.curtime);
            if (this.curtime == 0)
            {
                if (this._timeID > 0)
                {
                    clearInterval(this._timeID);
                    this._timeID = 0;
                }
            }
            return;
        }// end function

        private function showText(param1:int) : void
        {
            if (param1 < 0)
            {
                timeLabel.text = "剩余时间：00:00:00";
            }
            else if (this.day >= 1)
            {
                timeLabel.text = "剩余时间：" + TimeDataFormatUtil.getLeftTimeStr2(param1);
            }
            else
            {
                timeLabel.text = "剩余时间：" + TimeDataFormatUtil.getLeftTime2(param1);
            }
            return;
        }// end function

        private function showModel() : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_1:String = "";
            if (tab_shizhuang.selectedIndex == 0)
            {
                _loc_2 = ConfigDictionary.data.horse;
                _loc_1 = _loc_2[this.selectObj.model].model;
            }
            else if (tab_shizhuang.selectedIndex == 1)
            {
                _loc_3 = UserData.getUserData("sex");
                _loc_1 = this.selectObj.model[_loc_3];
            }
            if (this.oldModel == _loc_1)
            {
                return;
            }
            this.clearModel();
            this.model = new RoleShow(_loc_1, 4, null, null, null);
            if (tab_shizhuang.selectedIndex == 1)
            {
                this.model.x = 173 + 30 + 137;
                this.model.y = 240 + 40 + 178;
            }
            else
            {
                this.model.x = 173 + 30;
                this.model.y = 240 + 40;
            }
            this.addChild(this.model);
            this.model.mouseEnabled = false;
            this.model.mouseChildren = false;
            if (tab_shizhuang.selectedIndex == 0 && this.zuoqiArr.length > 0 || tab_shizhuang.selectedIndex == 1 && this.huanwuArr.length > 0)
            {
                this.model.filters = null;
            }
            else
            {
                this.model.filters = [FilterDefine.grayFilter];
            }
            this.oldModel = _loc_1;
            return;
        }// end function

        private function showCurShuXing() : void
        {
            var _loc_1:String = "";
            if (this.jihuoList.length > 0)
            {
                if (this.selectObj.atkmax > 0)
                {
                    _loc_1 = _loc_1 + ("物理攻击: " + "<font color=\'#ffe4c7\'>" + this.selectObj.atkmin + " - " + this.selectObj.atkmax + "</font>\n");
                }
                if (this.selectObj.magicmax > 0)
                {
                    _loc_1 = _loc_1 + ("魔法攻击: " + "<font color=\'#ffe4c7\'>" + this.selectObj.magicmin + " - " + this.selectObj.magicmax + "</font>\n");
                }
                if (this.selectObj.taomax > 0)
                {
                    _loc_1 = _loc_1 + ("道术攻击: " + "<font color=\'#ffe4c7\'>" + this.selectObj.taomin + " - " + this.selectObj.taomax + "</font>\n");
                }
                if (this.selectObj.defmax > 0)
                {
                    _loc_1 = _loc_1 + ("物理防御: " + "<font color=\'#ffe4c7\'>" + this.selectObj.defmin + " - " + this.selectObj.defmax + "</font>\n");
                }
                if (this.selectObj.mdefmax > 0)
                {
                    _loc_1 = _loc_1 + ("魔法防御: " + "<font color=\'#ffe4c7\'>" + this.selectObj.mdefmin + " - " + this.selectObj.mdefmax + "</font>\n");
                }
                if (this.selectObj.attackSpeed > 0)
                {
                    _loc_1 = _loc_1 + ("攻击速度: " + "<font color=\'#ffe4c7\'>" + Number(100000 / this.selectObj.attackSpeed).toFixed(0) + "</font>\n");
                }
                if (this.selectObj.expadd > 0)
                {
                    _loc_1 = _loc_1 + ("经验加成: " + "<font color=\'#ffe4c7\'>" + String(this.selectObj.expadd / 100) + "%</font>\n");
                }
            }
            else
            {
                _loc_1 = "物理攻击: 0\n" + "魔法攻击: 0\n" + "道术攻击: 0\n" + "物理防御: 0\n" + "魔法防御: 0\n" + "攻击速度: 0\n" + "经验加成: 0%";
            }
            curLabel.text = _loc_1;
            return;
        }// end function

        private function showCurLimit(param1:int) : void
        {
            var _loc_2:Object = {rein:0, open_level:0, guanzhi:0};
            tujingLabel.text = this.cfgFashion[(param1 + 1)].limit;
            chakanLabel.label = this.cfgFashion[(param1 + 1)].limit;
            if (tab_shizhuang.selectedIndex == 1)
            {
                tujingLabel.visible = true;
                chakanLabel.visible = false;
            }
            else
            {
                tujingLabel.visible = false;
                chakanLabel.visible = true;
            }
            return;
        }// end function

        private function rideHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new Output();
            _loc_2.writeVarInt(this.selectObj.id, false);
            if (rideBtn.label == "使用")
            {
                this.isRide = false;
                PluginManager.instance.sendToBufferServer("fashion", CMDFashion.PutOn, _loc_2);
                ModelProxy.change("mainview", "hideZuoqiBtn", 0);
            }
            else
            {
                this.isRide = true;
                PluginManager.instance.sendToBufferServer("fashion", CMDFashion.PutDown, _loc_2);
                ModelProxy.change("mainview", "hideZuoqiBtn", 1);
            }
            return;
        }// end function

        private function showTotalShuXing() : void
        {
            var _loc_20:int = 0;
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            var _loc_14:int = 0;
            var _loc_15:int = 0;
            var _loc_16:Array = [];
            var _loc_17:int = 0;
            while (_loc_17 < this.zuoqiArr.length)
            {
                
                _loc_16.push(this.zuoqiArr[_loc_17]);
                _loc_17++;
            }
            var _loc_18:int = 0;
            while (_loc_18 < this.huanwuArr.length)
            {
                
                _loc_16.push(this.huanwuArr[_loc_18]);
                _loc_18++;
            }
            if (_loc_16.length > 0)
            {
                _loc_20 = 0;
                while (_loc_20 < _loc_16.length)
                {
                    
                    _loc_1 = _loc_1 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].atkmin);
                    _loc_2 = _loc_2 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].atkmax);
                    _loc_3 = _loc_3 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].magicmin);
                    _loc_4 = _loc_4 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].magicmax);
                    _loc_5 = _loc_5 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].taomin);
                    _loc_6 = _loc_6 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].taomax);
                    _loc_7 = _loc_7 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].defmin);
                    _loc_8 = _loc_8 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].defmax);
                    _loc_9 = _loc_9 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].mdefmin);
                    _loc_10 = _loc_10 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].mdefmax);
                    _loc_11 = _loc_11 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].hppercent);
                    _loc_12 = _loc_12 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].mppercent);
                    if (int(this.cfgFashion[_loc_16[_loc_20].fashionId].attackSpeed) > 0)
                    {
                        _loc_13 = _loc_13 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].attackSpeed);
                    }
                    _loc_14 = _loc_14 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].nbvalue);
                    _loc_15 = _loc_15 + int(this.cfgFashion[_loc_16[_loc_20].fashionId].expadd);
                    _loc_20++;
                }
            }
            var _loc_19:String = "";
            _loc_19 = "" + ("物理攻击: " + "<font color=\'#ffe4c7\'>" + _loc_1 + " - " + _loc_2 + "</font>\n");
            _loc_19 = _loc_19 + ("魔法攻击: " + "<font color=\'#ffe4c7\'>" + _loc_3 + " - " + _loc_4 + "</font>\n");
            _loc_19 = _loc_19 + ("道术攻击: " + "<font color=\'#ffe4c7\'>" + _loc_5 + " - " + _loc_6 + "</font>\n");
            _loc_19 = _loc_19 + ("物理防御: " + "<font color=\'#ffe4c7\'>" + _loc_7 + " - " + _loc_8 + "</font>\n");
            _loc_19 = _loc_19 + ("魔法防御: " + "<font color=\'#ffe4c7\'>" + _loc_9 + " - " + _loc_10 + "</font>\n");
            if (_loc_13 > 0)
            {
                _loc_19 = _loc_19 + ("攻击速度: " + "<font color=\'#ffe4c7\'>" + Number(100000 / _loc_13).toFixed(0) + "</font>\n");
            }
            _loc_19 = _loc_19 + ("经验加成: " + "<font color=\'#ffe4c7\'>" + String(_loc_15 / 100) + "%</font>\n");
            totalLabel.text = _loc_19;
            this.mPowerNumber.number = _loc_14;
            return;
        }// end function

        private function chakanHandler(event:MouseEvent) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            if (ItemUilt.checkMapIsFly())
            {
                _loc_2 = ConfigDictionary.data.sysnpc[121];
                _loc_3 = ItemUilt.getAllCount(101);
                if (_loc_3 > 0)
                {
                    PluginManager.instance.sendToServer("activity", CMDActivity.Trans, {x:_loc_2.x, y:_loc_2.y, tostageid:_loc_2.stageid, free:1});
                }
                else
                {
                    ModelProxy.change("zone", "autoMove", {x:_loc_2.x, y:_loc_2.y, tostageid:_loc_2.stageid, onhorse:true});
                }
                ModelProxy.change("data", "taskfindpath", _loc_2.npcid);
                PluginManager.instance.closePlugin("role");
            }
            return;
        }// end function

        private function closeHandler(event:MouseEvent) : void
        {
            rightCon.visible = false;
            return;
        }// end function

        public function clearModel() : void
        {
            if (this.model)
            {
                this.model.destroy();
                this.model = null;
            }
            return;
        }// end function

    }
}
